﻿using Blog.Model;
using Blog.Model.Entitys;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Reflection;

namespace Blog.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ToolController : Controller
    {
        public ISqlSugarClient _db;
        public ToolController(ISqlSugarClient db)
        {
            _db = db;
        }

        [HttpGet]
        public ApiResult InitDateBase()
        {
            var result = new ApiResult();
            //如果不存在则创建数据库
            _db.DbMaintenance.CreateDatabase();
            //创建表
            string nspace = "Blog.Model.Entitys";
            //通过反射读取我们想要的类
            Type[] ass = Assembly.LoadFrom(AppContext.BaseDirectory + "Blog.Model.dll").GetTypes().Where(p => p.Namespace == nspace).ToArray();
            _db.CodeFirst.SetStringDefaultLength(200).InitTables(ass);

            //初始化炒鸡管理员和菜单
            Manager user = new Manager()
            {
                Account = "admin",
                NickName = "管理员",
                Password = "111",
                UserType = 0,
                IsEnable = true,
                BriefIntroduction = "有个鬼",
            };

            _db.Insertable(user).ExecuteCommand();

            //Menu menuRoot = new Menu()
            //{
            //    Name = "菜单管理",
            //    Index = "menumanager",
            //    FilePath = "../views/admin/menu/MenuManager",
            //    ParentId = 0,
            //    Order = 0,
            //    IsEnable = true,
            //    Description = "数据库初始化时默认添加的默认菜单",
            //    CreateDate = DateTime.Now,
            //    CreateUserId = userId,
            //    IsDeleted = 0
            //};
            //_db.Insertable(menuRoot).ExecuteReturnBigIdentity();

            return result.OK();
        }

    }
}
